/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { TextEditor } = require("./XTextInput");

class XStat {
    static GetStat(statPoint) {
        if (statPoint && statPoint < XStat.STAT_STACK.length) {
            return XStat.STAT_STACK[statPoint];
        }
        return XStat.STAT_STACK[XStat.STAT_STACK.length - 1];
    }
}

XStat.STAT_STACK = [];

XStat.pushStat = function (obj) {
    XStat.STAT_STACK.push(obj);
    TextEditor.NormalTextInput.Hide();
    return XStat.STAT_STACK.length - 1;
}
XStat.LastStat = () => {
    return XStat.STAT_STACK[XStat.STAT_STACK.length - 1];
}
XStat.CloseNut = function (nut) {
    for (let i = XStat.STAT_STACK.length - 1; i >= 0; i--) {
        if (XStat.STAT_STACK[i].nut_ && XStat.STAT_STACK[i].nut_ == nut) {
            XStat.STAT_STACK.splice(i, 1);
            return;
        }
    }
}
XStat.popStat = function (name) {
    if (name) {
        for (let i = XStat.STAT_STACK.length - 1; i >= 0; i--) {
            if (XStat.STAT_STACK[i].name_ == name) {
                for (let j = i; j < XStat.STAT_STACK.length - 1; j++) {
                    XStat.STAT_STACK[j] = XStat.STAT_STACK[j + 1];
                }
                XStat.popStat();
                return;
            }
        }
    }
    else {
        if (XStat.STAT_STACK.length > 1) {
            let p = XStat.STAT_STACK.pop();
            if (p.fullScreen_) {
            }
            TextEditor.NormalTextInput.Hide();
        }
    }
}
XStat.ResetStat = function (obj) {
    XStat.STAT_STACK = [obj];
}
XStat.SearchStat = function (name) {
    for (let i = XStat.STAT_STACK.length - 1; i >= 0; i--) {
        if (XStat.STAT_STACK[i].name_ == name) {
            return XStat.STAT_STACK[i];
        }
    }
    return null;
}
XStat.RecursiveStat = function (func, backward) {
    let i;
    for (i = XStat.STAT_STACK.length - 1; i >= 0; i--) {
        if (backward) {//事件
            XStat.OFFX = XStat.STAT_STACK[i].x_;
            XStat.OFFY = XStat.STAT_STACK[i].y_;
            if (func(XStat.STAT_STACK[i])) {
                return;
            }
        }
        else {//绘制
            if (XStat.STAT_STACK[i].fullScreen_) {
                for (i; i < XStat.STAT_STACK.length; i++) {
                    func(XStat.STAT_STACK[i]);
                }
                return;
            }
        }
    }
}

module.exports = {
    XStat
}